function h5Routes() {
  const context = require.context('../../router/h5', true, /^\.\/.+\.ts$/)
  const routes: any[] = []
  context.keys().forEach((key) => {
    routes.push(...context(key).default)
  })
  return routes
}
const defualtCacheRouter = () => {
  const routerList = h5Routes()
  const cacheRouter = routerList.filter((v) => {
    return v.meta && v.meta.keepAlive === true
  })
  const routerNames = cacheRouter.map((v) => v.name)
  return routerNames
}
const defautlNames = defualtCacheRouter()

const routerKeep = {
  state: {
    cachedViews: defautlNames, // 可缓存的组件名称
    excludedViews: [] // 不可缓存的组件名称
  },
  mutations: {
    // 移除指定缓存
    removeInclude(state: { cachedViews: string[] }, name: string): void {
      const index = state.cachedViews.findIndex((str) => str === name)
      state.cachedViews.splice(index, 1)
    },
    // 添加指定缓存
    addToInclude(state: { cachedViews: string[] }, name: string): void {
      state.cachedViews.push(name)
    },
    // 清除所有的缓存
    clearInclude(state: { cachedViews: string[] }): void {
      state.cachedViews = []
    },
    // 初始化指定缓存:路由名称
    initInclude(state: { cachedViews: string[] }, names: string[]): void {
      state.cachedViews = names || defautlNames
    },
    removeCache(state: { excludedViews: string[]; cachedViews: string[] }, name: string) {
      const index = state.excludedViews.findIndex((v) => v === name)
      if (index === -1) {
        // state.cachedViews.splice(index, 1)
        state.excludedViews.push(name)
      }
    },
    resetCache(state: { excludedViews: string[]; cachedViews: string[] }, name: string): void {
      /* const index = state.cachedViews.findIndex((v) => v === name)
      if (index === -1) {
        state.cachedViews.push(name)
      } */
      state.excludedViews = state.excludedViews.filter((item) => item !== name)
    },
    clearExclude(state: { excludedViews: string[] }): void {
      state.excludedViews = []
    }
  }
}
export default { ...{ namespaced: true }, ...routerKeep }
